<?php
$error = false;
$message = '';
try{
	$profileName = $arrDomainUri[ 1 ];
	$secondLevel = isset( $arrDomainUri[ 2 ] ) ? $arrDomainUri[ 2 ] : '';
	
	$userFilePath = '';
	$userModuleFile = array(  'expenses' => 'expenses', 'expense-add' => 'expense_add', 
		'dashboard' => 'dashboard', 'dashboard-b' => 'user_dashboard_b', 'report' => 'report',
		'team-report' => 'team_report' );
	
	if( key_exists( $secondLevel, $userModuleFile ) ){
		$userFilePath = $userModuleFile[ $secondLevel ];
	} else {
		//require_once 'macro.php';
		// this line is just to run dashboard page by default 
		$userFilePath = 'dashboard';
	}
	
	$userObj = getMultiple( 'user', "login||$profileName", "status||verified" );
	if( $userObj->getResultcount() < 1 ) {
		throw new Exception( "User not found!" );
	}

	$user = array( 'name' => $userObj->getName(), 
			'link' => 'http://' . $userObj->getLogin() . '.' . HOST_NAME,
			'lastActiveTime' => $userObj->getLastActiveTime(),
			'secondLastActiveTime' => $userObj->getSecondLastActiveTime() );

	$profileId = $userObj->getUserId();
	$userLink = 'http://' . $profileName . '.' . HOST_NAME;
	
	$canEdit = false;
	if( $wtLoginUserId == $profileId ){
		$canEdit = true;
	}

	if( isset( Url::$pageVars[ 'POST' ][ 'sendMessage' ] ) &&  $wtLoginUserId > 0 ){
		try{
			Conversation::sendMessage( $profileId, Url::$pageVars[ 'POST' ] );
		}catch( Exception $ex ){
			$error = true;
			$message = $ex->getMessage();
		}
	}
	
	/* code for navigation */
	$navOptArr = array( 'userId' => $profileId, 'canEdit' => $canEdit, 'upgraded' => $userUpgraded, 
		'currentNav' => $secondLevel, 'userLink' => $userLink );
	$leftNav = array();
	//describe( $leftNav, true );
	
	if( $userFilePath != '' ){
		if( $userFilePath == 'dashboard' && $wtLoginUserId != $profileId ){
			//$userFilePath = 'dashboard_b';
		}

		$template = new PHPTAL( TPL_PATH . 'user/' . $userFilePath . '.tpl.php' );
		require_once( PHP_PATH . 'user/' . $userFilePath . '.php' );
		
		// set the page title
		$template->title = "UREXPENSES PROFILE - " . strtoupper( $user[ 'name' ] );
		$template->welcomeMsg = "UREXPENSES PROFILE - " . strtoupper( $user[ 'name' ] );
		// fill user's data
		$template->user = $user;
		// message area
		$template->message = $message;

		$template->userLink = $userLink;
		$template->canEdit = $canEdit;
		$template->user = $user;
		$template->leftNav = $leftNav;
		$template->echoExecute();
		exit;
	}


	// set the page title
	$template->title = "UREXPENSES PROFILE - " . strtoupper( $user[ 'name' ] );
	$template->welcomeMsg = "UREXPENSES PROFILE - " . strtoupper( $user[ 'name' ] );
	// fill user's data
	$template->user = $user;
	// message area
	$template->message = $message;

	$template->userMacroTpl = TPL_PATH . "user/";
	$template->canEdit = $canEdit;
	$template->userLink = $userLink;
	$template->user = $user;
	$template->leftNav = $leftNav;
}catch( Exception $ex ){
	$error = true;
	$message = $ex->getMessage();
	describe( $message, true );
}
$template->message = $message;
$template->error = $error;